package com.zzyl.nursing.controller.member;

import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.service.IReservationService;
import com.zzyl.nursing.vo.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 预约信息Controller
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/member/reservation")
@Api(tags =  "预约信息相关接口")
public class MemberReservationController extends BaseController
{

    @Autowired
    private IReservationService reservationService;
    @GetMapping("/cancelled-count")
    @ApiOperation("查询取消预约数量")
    public AjaxResult getCancelledReservationCount() {

        //查询当前用户取消预约的次数，如果次数超过3次，则不允许再预约
        Long userId = UserThreadLocal.getUserId();
        Integer count=reservationService.getCancelledReservationCount(userId);
        return success(0);
    }


//    @GetMapping("/countByTime")
//    @ApiOperation("查询每个时间段剩余预约次数")
//    public R<Integer> getResidueCountByTime(Long time) {
//
//
//          reservationService.getResidueCountByTime(LocalDateTimeUtil.of(time));
//        return R.ok();
//    }



    /**
     * 查询预约信息列表
     */
//    @PreAuthorize("@ss.hasPermi('nursing:reservation:list')")
    @GetMapping("/page")
    @ApiOperation(value = "获取预约信息列表")
    public AjaxResult list( @RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize, @RequestParam(value = "status" ,required = false) Integer status)
    {

       PageResult<Reservation> list = reservationService.selectReservationList(pageNum,pageSize,status);


        return success(list);
    }



    /**
     * 获取预约信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('nursing:reservation:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取预约信息详细信息")
    public R<Reservation> getInfo(@ApiParam(value = "预约信息ID", required = true)
                                  @PathVariable("id") Long id)
    {
        return R.ok(reservationService.selectReservationById(id));
    }

    /**
     * 新增预约信息
     */
//    @PreAuthorize("@ss.hasPermi('nursing:reservation:add')")
//    @Log(title = "预约信息", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增预约信息")
    public AjaxResult add(@ApiParam(value = "预约信息实体", required = true) @RequestBody Reservation reservation)
    {
        return toAjax(reservationService.insertReservation(reservation));
    }

    /**
     * 修改预约信息
     */
    @PreAuthorize("@ss.hasPermi('nursing:reservation:edit')")
//    @Log(title = "预约信息", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改预约信息")
    public AjaxResult edit(@ApiParam(value = "预约信息实体", required = true) @RequestBody Reservation reservation)
    {
        return toAjax(reservationService.updateReservation(reservation));
    }

    /**
     * 删除预约信息
     */
    @PreAuthorize("@ss.hasPermi('nursing:reservation:remove')")
//    @Log(title = "预约信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("删除预约信息")
    public AjaxResult remove(@ApiParam(value = "要删除的数据id的数组") @PathVariable Long[] ids)
    {
        return toAjax(reservationService.deleteReservationByIds(ids));
    }



    @PutMapping("/{id}/cancel")
    @ApiOperation("取消预约")
    public AjaxResult cancel(@ApiParam(value = "要取消的预约id") @PathVariable Long id){

        reservationService.cancle(id);
        return success();

    }

}